/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.nts.pointsstore;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

/**
 *
 * @author Himagirinathan
 */
public class PointsStore extends Activity implements OnClickListener
{
  Button blogin;
  EditText usrname,pwd;
  private DataHelper dh;

  /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
    	try
    	{
         super.onCreate(icicle);
         this.setContentView(R.layout.pointsstore);
         //ConnectToDB ctdb=new ConnectToDB(this);
         /*
         Display display = getWindowManager().getDefaultDisplay();
         int width = display.getWidth();
         int height = display.getHeight();
         */
         usrname=(EditText)this.findViewById(R.id.username);
         pwd=(EditText)this.findViewById(R.id.password);
         blogin=(Button)this.findViewById(R.id.login);
         blogin.setOnClickListener(this);
         dh = new DataHelper(this);
         if(dh.countUsers()==0)
          dh.insertUser("and","roid","and@roid.com","Active",10);
    	}
    	catch(Exception e)
    	{
    		AlertDialog.Builder ad=new Builder(this);
            ad.setMessage(e.toString());
            ad.setNeutralButton("Ok", null);
            ad.show();
    	}
    }

    public void onClick(View v)
    {
     try
     {
      if(v==blogin)
      {
       String strusrname=usrname.getText().toString();
       String strpwd=pwd.getText().toString();
        if(!("".equals(strusrname)||"".equals(strpwd)))
        {
          long id=dh.selectUser(strusrname,strpwd);
          if(id!=-1)
          {
            Intent it=new Intent(this, UserHome.class);
            Bundle bundle = new Bundle();
            bundle.putLong("id",id);
            it.putExtras(bundle);
            this.startActivity(it);
          }
          else
          {
            AlertDialog.Builder ad=new Builder(this);
            ad.setMessage("UserName or Password Did not Match.");
            ad.setNeutralButton("Ok", null);
            ad.show();
          }
        }
      }
     }
     catch(Exception e)
     {
    	 AlertDialog.Builder ad=new Builder(this);
         ad.setMessage(e.toString());
         ad.setNeutralButton("Ok", null);
         ad.show();
     }
      
    }
}
